/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : product_shell.c
 * Author        : joki.zhu
 * Date          : 2019-09-19
 *
 * Record        :
 * 1.Date        : 2019-09-19
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/

#include "sys/types.h"
#include "linux/kernel.h"
#include "shell.h"
#include "los_event.h"
#include "los_task.h"
#include "stdio.h"
#include "lwip/netif.h"
#include "src/common/defs.h"
#include "app_common.h"
#include "config_management.h"
#include "ProductManager.h"
#include "DevGpio.h"
#include "DevLed.h"
#include "hi_uartmsg.h"
#include "Video.h"
#include "Audio.h"
#include "System.h"
#include "Net.h"
#include "PowerManager.h"
#include "DevFlash.h"
#include "partition_table.h"
#include "Upgrade.h"
#include "DevAdc.h"
#include "DevWifi.h"
#include "DevConfig.h"
#include "OsdDraw.h"
#include "DevSd.h"
#include "DevSocSub.h"
#include <sys/stat.h>

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

extern void app_control(int argc, char **argv );

static int cmd_logset(int argc, char* argv[])
{
    if (3 != argc)
    {
        printf(
                "\n"
                "*************************************************\n"
                "Usage: ./logset [LogOn] [LogTimeOn] [LogLevel]\n"
                "LogLevel: \n"
                "   HI_LOG_LEVEL_FATAL: %d\n"
                "   HI_LOG_LEVEL_ERROR: %d\n"
                "   HI_LOG_LEVEL_WARNING: %d\n"
                "   HI_LOG_LEVEL_INFO: %d\n"
                "   HI_LOG_LEVEL_DEBUG: %d\n"
                "e.g : ./logset 1 1 3\n"
                "*************************************************\n"
                "\n", HI_LOG_LEVEL_FATAL, HI_LOG_LEVEL_ERROR, HI_LOG_LEVEL_WARNING,
                HI_LOG_LEVEL_INFO, HI_LOG_LEVEL_DEBUG);
        return HI_FAILURE;
    }
    HI_BOOL bLogOn = atoi(argv[0]);
    HI_BOOL bLogTimeOn = atoi(argv[1]);
    HI_LOG_LEVEL_E enLogLevel = atoi(argv[2]);
    HI_APPCOMM_CHECK_EXPR(((enLogLevel >= HI_LOG_LEVEL_FATAL) && (enLogLevel < HI_LOG_LEVEL_BUTT)) ,HI_FAILURE);
    MLOGD("LogOn[%d] LogTimeOn[%d] Level[%d]\n", bLogOn, bLogTimeOn, enLogLevel);
    HI_LOG_Config(bLogOn, bLogTimeOn, enLogLevel);
    return HI_SUCCESS;
}

static int cmd_gpioset(int argc, char *argv[])
{
    if (argc != 2) {
        printf(
        	"\n"
        	"*************************************************\n"
        	"Usage: ./gpioset [GpioIndex] [set/reset]\n"
        	"GpioIndex: \n"
        	"PRODUCT_GPIO_PHOTORESISTANCE: %d\n"      
        	"PRODUCT_GPIO_IRCUT0: %d\n"                
        	"PRODUCT_GPIO_IRCUT1: %d\n"                
        	"PRODUCT_GPIO_SPEAK_ENABLE: %d\n"       
        	"PRODUCT_GPIO_LED_BLUE: %d\n"         
        	"PRODUCT_GPIO_LED_RED: %d\n"              
        	"PRODUCT_GPIO_KEY_RESET_WIFI_CONFIG: %d\n"
        	"PRODUCT_GPIO_INFRAREDLAMP: %d\n"         
        	"PRODUCT_GPIO_WIFI_PWDN: %d\n"            
        	"PRODUCT_GPIO_WIFI_WAKE: %d\n"
        	"PRODUCT_GPIO_KEEP_SELF_POWER: %d\n"
        	"PRODUCT_GPIO_RESET_MCU: %d\n"
        	"*************************************************\n"
        	"\n", PRODUCT_GPIO_PHOTORESISTANCE, PRODUCT_GPIO_IRCUT0, PRODUCT_GPIO_IRCUT1,PRODUCT_GPIO_SPEAK_ENABLE,
        	PRODUCT_GPIO_LED_BLUE, PRODUCT_GPIO_LED_RED, PRODUCT_GPIO_KEY_RESET_WIFI_CONFIG,
        	PRODUCT_GPIO_INFRAREDLAMP, PRODUCT_GPIO_WIFI_PWDN, PRODUCT_GPIO_WIFI_WAKE,
        	PRODUCT_GPIO_KEEP_SELF_POWER, PRODUCT_GPIO_RESET_MCU);

        	return -1;
    }
	
	if (!strcmp(argv[1], "set"))
	{
		GpioSet(atoi(argv[0]));
	}
	else if (!strcmp(argv[1], "reset"))
	{
		GpioReset(atoi(argv[0]));
	}

	return 0;
}


static int cmd_ledset(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    ledset [red/blue] [0/1/2/3/4]\n");
        printf("    ledset upgrade\n");
        printf("    ledset level reset\n");
        return -1;
    }

    if (argc == 2)
    {
     	if (!strcmp(argv[0], "red"))
    	{
    		LedSet(LED_TYPE_RED, atoi(argv[1]), LED_LEVEL_LOW);
    	}
    	else if (!strcmp(argv[0], "blue"))
    	{
    		LedSet(LED_TYPE_BLUE, atoi(argv[1]), LED_LEVEL_LOW);
    	}
    	else if (!strcmp(argv[0], "level"))
    	{
    	    if (!strcmp(argv[1], "reset"))
            {
               LedSet(LED_TYPE_NONE, LED_STATE_NONE, LED_LEVEL_LOW); 
            }
    	}        
    }
    else 
    {
    	if (!strcmp(argv[0], "upgrade"))
    	{
    		LedSet(LED_TYPE_RED, LED_STATE_UPGRADE, LED_LEVEL_HIGH);
    	}         
    }
    
	return 0;
}

static int cmd_waveset(int argc, char *argv[])
{
    char ssid[32] = "";
    char pwd[32] = "";
    
    extern int SoundWaveWifiProcess(char *ssid, char *pwd);
    SoundWaveWifiProcess(ssid, pwd);
    
    LOGI("ssid: %s pwd: %s\n", ssid, pwd);
    
	return 0;
}

static int cmd_pirset(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    pirset [on/off]     ---on/off pir\n");
        printf("    pirset get          ---get sensitivity\n");
        printf("    pirset set [value]  ---set sensitivity\n");
        return -1;
    }

    unsigned char val = 0;
             
	if (!strcmp(argv[0], "on"))
	{
	    val = 0x1;
		HI_UARTMSG_SendData(HI_UARTMSG_S2M_SET_PIR_SWITCH, &val, 1);
	}
	else if (!strcmp(argv[0], "off"))
	{
	    val = 0x0;
		HI_UARTMSG_SendData(HI_UARTMSG_S2M_SET_PIR_SWITCH, &val, 1);
	}
    else if (!strcmp(argv[0], "get"))
    {
        int value = 0;
        SocSubGetPirSensitivity(&value);
    }
    else if (!strcmp(argv[0], "set"))
    {
        if (argc != 2) {
            LOGD("no found argv[1]\n");
            return -1;
        }
        int value = atoi(argv[1]);
        SocSubSetPirSensitivity(value);
    }
    
	return 0;
}

static int cmd_videoset(int argc, char *argv[])
{
    VideoGetOpsHandle()->request_idr_frame(VIDEO_CHN_MAIN);
	return 0;
}

static int cmd_osset(int argc, char *argv[])
{
    if (argc != 1) {
        printf("Usage:\n");
        printf("    osset [off/reset/update]\n");
        return -1;
    }

	if (!strcmp(argv[0], "off"))
	{
        HI_UARTMSG_SendData(HI_UARTMSG_S2M_SOC_POWER_DOWN, NULL, 0);
	}
	else if (!strcmp(argv[0], "reset"))
	{
	    SystemReboot("osset cmd reset");
	}
	else if (!strcmp(argv[0], "update"))
	{
	    SystemSetUpdateFlag();
	    SystemReboot("osset cd update");
	}   
    
	return 0;
}

static int cmd_daemon(int argc, char *argv[])
{
    extern int daemon_run(void);
    daemon_run();
    
	return 0;
}

void *tskPlayAudioTest(void *args)
{
    pthread_detach(pthread_self());

    char *fileName = (char *)args;
    
    FILE *fp = fopen(fileName, "rb");
    if (fp == NULL) {
        LOGE("fopen failed\n");
        return NULL;
    }
    
    int ret = 0;
    int readLen = 0;
    unsigned char dataBuf[480] = {0};

    AudioTalkPlaySet(0, 0, 0, 0);
    
    while (1)
    {
        memset(dataBuf, 0, sizeof(dataBuf));
        readLen = fread(dataBuf, 1, sizeof(dataBuf), fp);
        if (readLen <= 0) {
            printf("read file over\n");
            break;
        }

        //ret = AudioTalkPlayFrame(dataBuf, readLen);
        ret = AudioTalkPlayData(dataBuf, readLen, AUDIO_TYPE_G711A);
        if (ret < 0) {
            printf("AudioTalkPlayFrame failed\n");
            break;
        }
    }

    fclose(fp);
    AudioTalkPlayFinish();    

    pthread_exit(0);
}

static int cmd_playaudio(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    playauio [pcm_file]\n");
        return -1;
    }
#if 1
    int ret = 0;
    pthread_t pid = 0;
    ret = pthread_create(&pid, NULL, (void *)tskPlayAudioTest, (void *)argv[0]);
    if (ret != 0) {
        LOGE("pthread_create failed\n");
        return -1;
    }
#else
    FILE *fp = fopen(argv[0], "rb");
    if (fp == NULL) {
        perror(argv[1]);
        return -1;
    }

    int ret = 0;
    int readLen = 0;
    unsigned char dataBuf[480] = {0};

    AudioTalkPlaySet(0, 0, 0, 0);
    
    while (1)
    {
        memset(dataBuf, 0, sizeof(dataBuf));
        readLen = fread(dataBuf, 1, sizeof(dataBuf), fp);
        if (readLen <= 0) {
            printf("read %s file over\n", argv[0]);
            break;
        }

        ret = AudioTalkPlayFrame(dataBuf, readLen);
        if (ret < 0) {
            printf("AudioTalkPlayFrame failed\n");
            break;
        }
    }

    fclose(fp);
    AudioTalkPlayFinish();    
 #endif
 
	return 0;
}

static int cmd_playg711a(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    playg711a [g711a_file]\n");
        return -1;
    }


    AudioTalkPlayStart();

    int readLen = 0;
    FILE *fp = NULL;
    unsigned char buf[512] = {0};
    
    fp = fopen(argv[0], "rb");
    if (fp == NULL) {
        LOGE("fopen failed\n");
        return -1;
    }

    while (1)
    {
        memset(buf, 0, sizeof(buf));
        readLen = fread(buf, 1, sizeof(buf), fp);
        if (readLen <= 0) {
            LOGW("read over!\n");
            break;
        }

        AudioTalkPlayData(buf, readLen, AUDIO_TYPE_G711A);
    }

    fclose(fp);
    AudioTalkPlayStop();
    
	return 0;
}

static int cmd_gethostip(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    gethostip [host]\n");
        return -1;
    }
    
    char ip[32] = "";

#if  0
    NetLiteosGetAddrInfo(argv[0], ip);
#else
    struct addrinfo * res, *pt;
    struct sockaddr_in *sinp;
    struct addrinfo hints;
    const char *addr;
    int succ=0,i=0;
    
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET; /* Allow IPv4 */
    hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
    hints.ai_protocol = 0; /* Any protocol */
    hints.ai_socktype = SOCK_STREAM;
    printf("getaddrinfo~~~~~~~~~~~~");
    succ = netliteos_getaddrinfo(argv[0], NULL, &hints, &res);
    if (succ != 0)
    {
        printf("lwip_getaddrinfo failed\n");
    }
    else
    {
        if (res->ai_addr->sa_family == AF_INET)
        {
           if (ip != NULL)
           {
               struct sockaddr_in* pServAddr = (struct sockaddr_in *)res->ai_addr;
               inet_ntop(AF_INET, &(pServAddr->sin_addr), ip, INET_ADDRSTRLEN);
           }
        }       
    }
#endif /* #if 0 */

    printf("ip: %s\n", ip);

    
	return 0;
}


static int cmd_wakelock(int argc, char *argv[])
{
    if (argc != 1) {
        printf("Usage:\n");
        printf("    wakelock [on/off]\n");
        return -1;
    }

	if (!strcmp(argv[0], "on"))
	{
        PowerSetWakeLock("Test");
	}
	else if (!strcmp(argv[0], "off"))
	{
	    PowerSetWakeUnLock("Test");
	}
    
	return 0;
}

static int cmd_ubootenv(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    ubootenv [read/write] [file]\n");
        return -1;
    }

    int ret = 0;
    unsigned long startaddr = 0x80000;
    unsigned long length = 0x40000;

	if (!strcmp(argv[0], "read"))
	{
	    if (argc == 2)
	    {
            unsigned char *pDataBuf = NULL;
            pDataBuf = (unsigned char *)malloc(length);
            if (pDataBuf == NULL) {
                LOGE("malloc failed\n");
                return -1;
            }
            ret = FlashGetOpsHandle()->flash_read(0, (void *)pDataBuf, startaddr, length);
            if (ret < 0) {
                printf("flash_write failed\n");
                free(pDataBuf);
                return -1;
            }
            
            FILE *fp = fopen(argv[1], "wb");
            if (fp == NULL) {
                LOGE("fopen %s failed\n", argv[1]);
                free(pDataBuf);
                return -1;
            }
            
            fwrite(pDataBuf, 1, length, fp);
            fclose(fp);
            free(pDataBuf);
	    }
	}
	else if (!strcmp(argv[0], "write"))
	{
	    if (argc == 2)
	    {
            FILE *fp = fopen(argv[1], "rb");
            if (fp == NULL) {
                LOGE("fopen %s failed\n", argv[1]);
                return -1;
            }

            unsigned char *pDataBuf = NULL;
            pDataBuf = (unsigned char *)malloc(length);
            if (pDataBuf == NULL) {
                LOGE("malloc failed\n");
                fclose(fp);
                return -1;
            }

            fread(pDataBuf, 1, length, fp);
            fclose(fp);
            
            ret = FlashGetOpsHandle()->flash_erase(0, startaddr, length);
            if (ret < 0) {
                printf("flash_erase failed\n");
                free(pDataBuf);
                return -1;
            }
            ret = FlashGetOpsHandle()->flash_write(0, (void *)pDataBuf, startaddr, length);
            if (ret < 0) {
                printf("flash_write failed\n");
                free(pDataBuf);
                return -1;
            }
            free(pDataBuf);
	    }
	}

}

static int cmd_flashtest(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    flashtest [read/write] [data]\n");
        return -1;
    }

    int ret = 0;
    unsigned long startaddr = FLASH_RESERVED_OFFSET;
    unsigned long length = 0x10000;
    
	if (!strcmp(argv[0], "read"))
	{
	    unsigned char dataBuf[128] = {0};
        ret = FlashGetOpsHandle()->flash_read(0, (void *)dataBuf, startaddr, sizeof(dataBuf));
        if (ret < 0) {
            printf("flash_write failed\n");
            return -1;
        }
        printf("data: %s\n", dataBuf);
	}
	else if (!strcmp(argv[0], "write"))
	{
	    if (argc == 2)
	    {
            ret = FlashGetOpsHandle()->flash_erase(0, startaddr, length);
            if (ret < 0) {
                printf("flash_erase failed\n");
                return -1;
            }
            ret = FlashGetOpsHandle()->flash_write(0, (void *)argv[1], startaddr, length);
            if (ret < 0) {
                printf("flash_write failed\n");
                return -1;
            }            
	    }
	}

	return 0;
}


static int cmd_printtest(int argc, char *argv[])
{
    int i = 0;

    for (i = 0; i < 30; i++)
    {
        printf("\rInstall...%d", i);
        fflush(stdout);
        usleep(500 * 1000);
    }
    printf("\n");
    
	return 0;
}

static int cmd_update(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    update [file]\n");
        return -1;
    }

    int fileFd = 0;
    struct stat filestat;
    int blocks = 0;
    unsigned int erasesize = 0x10000;
    unsigned int size = 0;
    unsigned int written = 0;
    int i = 0;
    int ret = 0;
    int resultsize = 0;
    unsigned char buf[64 * 1024] = {0};
    
    fileFd = open(argv[1], O_RDONLY);
    if (fstat(fileFd, &filestat) < 0) {
        LOGE("fstat %s failed\n", argv[1]);
        return -1;
    }

    blocks = (filestat.st_size + erasesize - 1) / erasesize;

    for (i = 0; i < blocks; i++)
    {
        printf("blocks: %d\n", i);
        ret = FlashGetOpsHandle()->flash_erase(0, FLASH_APP_OFFSET + erasesize * i, (long)erasesize);
        if (ret < 0) {
            printf("flash_erase failed\n");
            goto close_fd;
        }

        resultsize = read(fileFd, buf, 64 * 1024);
        if (resultsize <= 0) {
            printf("resultsize <= 0\n");
            ret = -1;
            goto close_fd;
        }

        resultsize = FlashGetOpsHandle()->flash_write(0, buf, FLASH_APP_OFFSET + erasesize * i, resultsize);
    }

close_fd:
    close(fileFd);
	return ret;
}

#define ARGS_SIZE_T 20                 
#define ARG_BUF_LEN_T 256
static char *ptask_args[ARGS_SIZE_T];
static char *args_buf_t = NULL;
static unsigned int taskid = -1;  

void com_app(unsigned long p0, unsigned long p1, unsigned long p2, unsigned long p3)
{
    int i = 0;
    unsigned int argc = p0;
    char** argv = (char**)p1;
    //Set_Interupt(0);
    dprintf("\ninput command:\n");

    for (i = 0; i < argc; i++)
    {
        dprintf("%s ", argv[i]);
    }

    dprintf("\n");
    cmd_update(argc, argv);
    dprintf("\nmain out\n");
    dprintf("[END]:app_test finish!\n");
    free(args_buf_t);
    args_buf_t = NULL;
    taskid = -1;
}

unsigned int app_sample(int argc, char **argv )
{
    int i = 0;
    unsigned int ret = 0;
    int len = 0;
    char* pch = NULL;
    TSK_INIT_PARAM_S stappTask;

    if (argc < 1)
    {
        dprintf("illegal parameter!\n");
    }

    if (taskid != -1)
    {
        dprintf("There's a app_main task existed.");
    }

    args_buf_t = zalloc(ARG_BUF_LEN_T);
    memset(&stappTask, 0, sizeof(TSK_INIT_PARAM_S));
    pch = args_buf_t;

    for (i = 0; i < ARGS_SIZE_T; i++)
    {
        ptask_args[i] = NULL;
    }

    argc++;
    ptask_args[0] = "sdk_test";

    for (i = 1; i < argc; i++)
    {
        len = strlen(argv[i - 1]);
        memcpy(pch, argv[i - 1], len);
        ptask_args[i] = pch;
        //keep a '\0' at the end of a string.
        pch = pch + len + 1;

        if (pch >= args_buf_t + ARG_BUF_LEN_T)
        {
            dprintf("args out of range!\n");
            break;
        }
    }

    chdir("/sharefs");

    memset(&stappTask, 0, sizeof(TSK_INIT_PARAM_S));
    stappTask.pfnTaskEntry = (TSK_ENTRY_FUNC)com_app;
    stappTask.uwStackSize = 0x20000;
    stappTask.pcName = "sdk_test";
    stappTask.usTaskPrio = 10;
    stappTask.uwResved = LOS_TASK_STATUS_DETACHED;
    stappTask.auwArgs[0] = argc;
    stappTask.auwArgs[1] = (unsigned long)ptask_args;
    ret = LOS_TaskCreate(&taskid, &stappTask);

    dprintf("camera_Task %d\n", taskid);

    return ret;
}

#include "hi_upgrade.h"
static int cmd_upgrade(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    upgrade [pack_path]\n");
        return -1;
    }
    
    UpgradeInit(5 * 1024 * 1024);
    
    int ret = 0;
    if (argc >= 1)
    {
#if 1
        ret = UpgradeRun(argv[0]);
        if (ret < 0) {
            LOGE("UpgradeRun failed\n");
            goto err_exit;
        }
#else
        char devVersion[32] = {0};
        HI_UPGRADE_DEV_INFO_S devInfo;

        memset(&devInfo, 0, sizeof(devInfo));

        DevGetSoftVersion(devVersion);
        sprintf(devInfo.szSoftVersion, "%s", devVersion);
        sprintf(devInfo.szSoftTime, "%s", "B1");   
        
        ret = HI_UPGRADE_CheckPkt(argv[0], &devInfo);
        if (ret < 0) {
            LOGE("HI_UPGRADE_CheckPkt failed\n");
            return -1;
        }

        ret = HI_UPGRADE_DoUpgrade(argv[0]);
        if (ret < 0) {
            LOGE("HI_UPGRADE_DoUpgrade failed\n");
            return -1;
        }
#endif       
    }

err_exit:
    UpgradeDeinit();
	return ret;
}

#include "hal_upgrade.h"
#define PKT_BUF_SIZE        (5 * 1024 * 1024)
#define UPGRADE_BLOCK_SIZE  (0x10000)

static int cmd_upgrade3(int argc, char *argv[])
{
    HalVideoDeInit();
    if (argc == 0) {
        printf("Usage:\n");
        printf("    upgrade [pack_path] [out_path]\n");
        return -1;
    }

    int ret = 0;
    FILE *pktFp = NULL;
    int readLen = 0;
    int readOffset = 0;
    unsigned char *pPktBuf = NULL;
    if (argc >= 2)
    {
        /* init buf */
        pPktBuf = (unsigned char *)malloc(PKT_BUF_SIZE);
        if (pPktBuf == NULL) {
            LOGE("malloc %dM failed\n", PKT_BUF_SIZE/(1024*1024));
            return -1;
        }
        memset(pPktBuf, 0, PKT_BUF_SIZE);
        LOGI("malloc %dM success\n", PKT_BUF_SIZE/(1024*1024));


        /* read pkt to buffer */
        pktFp = fopen(argv[0], "rb");
        if (pktFp == NULL) {
            LOGE("fopen %s failed\n", argv[0]);
            goto free_buf;
        }

        while (1)
        {
            readLen = fread(pPktBuf + readOffset, 1, UPGRADE_BLOCK_SIZE, pktFp);
            if (readLen == 0) {
                fclose(pktFp);
                LOGI("fread %s over, read file size: %d\n", argv[0], readOffset);
                break;
            } else if (readLen < 0) {
                LOGE("fread %s failed\n", argv[0]);
                fclose(pktFp);
                goto free_buf;
            }
            readOffset += readLen;
        }

#if 0
        FILE *outFp = NULL;
        int writeSize = UPGRADE_BLOCK_SIZE;
        int writeLen = 0;
        int writeOffset = 0;
        int remainWriteLen = readOffset;
        
        outFp = fopen(argv[1], "wb");
        if (outFp == NULL) {
            LOGE("fopen %s failed\n", argv[1]);
            goto free_buf;
        }    
        while (remainWriteLen > 0)
        {
            writeSize = remainWriteLen > UPGRADE_BLOCK_SIZE ? UPGRADE_BLOCK_SIZE : remainWriteLen;
            writeLen = fwrite(pPktBuf + writeOffset, 1, writeSize, outFp);
            if (writeLen < 0) {
                LOGE("fwrite %s failed\n", argv[1]);
                fclose(outFp);
                goto free_buf;
            }
            remainWriteLen -= writeLen;
            writeOffset += writeLen;
        }
        sync();
        sync();
        fflush(outFp);
        fflush(outFp);
        fclose(outFp);
        LOGI("fwrite %s over, fwrite file size: %d\n", argv[1], writeOffset);
#endif


        /* run upgrade */
        HI_UPGRADE_DEV_INFO_S devInfo;
        memset(&devInfo, 0, sizeof(devInfo));
        sprintf(devInfo.szSoftVersion, "%s", "1.0.0.0");
        sprintf(devInfo.szSoftTime, "%s", "dashcam");   
    
        ret = HAL_UPGRADE_CheckPkt(pPktBuf, &devInfo);
        if (ret < 0) {
            LOGE("HI_UPGRADE_CheckPkt failed\n");
            goto free_buf;
        }
        ret = HAL_UPGRADE_DoUpgrade(argv[1], pPktBuf);
        if (ret < 0) {
            LOGE("HI_UPGRADE_DoUpgrade failed\n");
            goto free_buf;
        }
    }

free_buf:
    if (pPktBuf) {
        free(pPktBuf);
    }
	return 0;
} 

static int cmd_getbattery(int argc, char *argv[])
{
    BATTERY_STATUS_S stBatStatus;
   
	memset(&stBatStatus, 0, sizeof(stBatStatus));
    SocSubGetBatteryStatus(&stBatStatus);
   	printf("capacity: %d\n", stBatStatus.capacity); 

    return 0;
}

static int cmd_getwakeup(int argc, char *argv[])
{
    SYSTEM_WAKEUP_TYPE_E enWakeUpType = 0;

    SocSubGetWakeUpType(&enWakeUpType);
    
    return 0;
}

static int cmd_getsockopt(int argc, char *argv[])
{
    int z;
    int s=-1;            /* Socket */
    int sndbuf=0;        /* Send buffer size */
    int rcvbuf=0;        /* Receive buffer size */
    socklen_t optlen;        /* Option length */

    /*
     * Create a TCP/IP socket to use:
     */
    s = socket(PF_INET,SOCK_STREAM,0);
    if(s==-1)
    printf("socket(2)");

    /*
     * Get socket option SO_SNDBUF:
     */
    optlen = sizeof(sndbuf);
    z = getsockopt(s,SOL_SOCKET,SO_SNDBUF,&sndbuf,&optlen);

    if(z)
    printf("getsockopt(s,SOL_SOCKET,"
        "SO_SNDBUF)");

    //assert(optlen == sizeof(sndbuf));

    /*
     * Get socket option SON_RCVBUF:
     */

    optlen = sizeof(rcvbuf);
    z = getsockopt(s,SOL_SOCKET,SO_RCVBUF,&rcvbuf,&optlen);
    if(z)
    printf("getsockopt(s,SOL_SOCKET,"
        "SO_RCVBUF)");

    //assert(optlen == sizeof(rcvbuf));

    /*
     * Report the buffer sizes:
     */
    printf("Socket s: %d\n",s);
    printf("Send buf: %d bytes\n",sndbuf);
    printf("Recv buf: %d bytes\n",rcvbuf);

    close(s);

}

static int cmd_getadc(int argc, char *argv[])
{
    int i = 0;
    int ret = 0;
    int adcChn = 1;
    int adcValue = 0;

    ret = AdcInit();
    if (ret < 0) {
        LOGE("AdcInit failed\n");
        return -1;
    }

    for (i = 0; i < 50; i++)
    {
        adcValue = 0;
        ret = AdcGetVal(adcChn, &adcValue);
        if (ret < 0) {
            LOGE("AdcGetVal chn%d failed\n", adcChn);
           goto err_out;
        }

        printf("adcValue : %d\n", adcValue);
        usleep(100 * 1000);
    }

err_out:    
    ret = AdcDeinit();
    if (ret < 0) {
        LOGE("AdcDeinit failed\n");
        return -1;
    }
    
    return 0;
}



static int cmd_setvideo(int argc, char *argv[])
{
    if (argc != 1) {
        printf("Usage:\n");
        printf("    setvideo [1/2/3]\n");
        return -1;
    }

    VIDEO_STREAM_ATTR_S stStreamAttr;

    memset(&stStreamAttr, 0, sizeof(stStreamAttr));
	if (!strcmp(argv[0], "1"))
	{
	    stStreamAttr.width = 640;
	    stStreamAttr.height = 360;
	    stStreamAttr.bitrate = 320;
        stStreamAttr.framerate = 15;
        VideoGetOpsHandle()->set_resolution(0, stStreamAttr);
	}
	else if (!strcmp(argv[0], "2"))
	{
	    stStreamAttr.width = 1280;
	    stStreamAttr.height = 720;
	    stStreamAttr.bitrate = 512;
        stStreamAttr.framerate = 15;
        VideoGetOpsHandle()->set_resolution(0, stStreamAttr);

	}	
	else if (!strcmp(argv[0], "3"))
	{
	    stStreamAttr.width = 1920;
	    stStreamAttr.height = 1080;
	    stStreamAttr.bitrate = 1024;
        stStreamAttr.framerate = 15;
        VideoGetOpsHandle()->set_resolution(0, stStreamAttr);
	}
    
	return 0;
}


static int cmd_setstream(int argc, char *argv[])
{
    if (argc != 1) {
        printf("Usage:\n");
        printf("    setstream [0/1]\n");
        return -1;
    }

    if (!strcmp(argv[0], "1"))
    {
         HI_MW_VEnc_StreamSwitch(0, HI_TRUE);
    }
    else
    {
         HI_MW_VEnc_StreamSwitch(0, HI_FALSE);
    }

    return 0;
}

static int cmd_color2grey(int argc, char *argv[])
{
    if (argc != 1) {
        printf("Usage:\n");
        printf("    color2grey [0/1]\n");
        return -1;
    }

    

    if (!strcmp(argv[0], "1"))
    {
         VideoGetOpsHandle()->set_daynight_mode(0, VIDEO_NIGHT_ON);
    }
    else
    {
         VideoGetOpsHandle()->set_daynight_mode(0, VIDEO_NIGHT_OFF);
    }

    return 0;
}

static int cmd_getwifistatus(int argc, char *argv[])
{
    WIFI_STATUS_INFO_S stWifiStatusInfo;

    memset(&stWifiStatusInfo, 0, sizeof(stWifiStatusInfo));
    WifiGetStatus(&stWifiStatusInfo);
    printf("status:%d ssid:%s singnal:%d\n", stWifiStatusInfo.connectionStatus, stWifiStatusInfo.ssid, stWifiStatusInfo.signalLevel);

    return 0;
}

static int cmd_sleepset(int argc, char *argv[])
{
    if (argc != 1) {
        printf("Usage:\n");
        printf("    1 - [ip] [port] create sleep heartbeat\n");
        printf("    2 - wifi sleep\n");
        printf("    3 - create sleep heartbeat and close power\n");
        return -1;
    }

    if (!strcmp(argv[0], "1"))
    {
        int ret = 0;
        WIFI_SLEEP_ATTR_S stSleepAttr;
        
        //获取WiFi休眠配置
        memset(&stSleepAttr, 0, sizeof(stSleepAttr));
        ret = WifiGetSleepConfig(&stSleepAttr); 
        if (ret < 0) {
            LOGE("WifiGetSleepConfig failed\n");
        }
        
        //设置WiFi休眠心跳
        ret = WifiSetSleepHeartBeat(stSleepAttr);
        if (ret < 0) {
            LOGE("WifiSetSleepHeartBeat2 failed\n");
            return -1;
        }
    }
    else if (!strcmp(argv[0], "2"))
    {
         WifiSetSleepMode();
    }
    else if (!strcmp(argv[0], "3"))
    {
         WifiSetSleepMode();
         SocSubSetMainSocPowerOff();
    }
    

    return 0;
}

static int gs_totalNum = 0;
void *foo(void)
{
    pthread_detach(pthread_self());

    gs_totalNum++;

    int id = gs_totalNum;
    
    while (1)
    {
        LOGD("id %d\n", id);
        sleep(3);
    }
}

static int cmd_threadtest(int argc, char *argv[])
{
    if (argc != 1) {
        printf("Usage:\n");
        printf("    threadtest [num]\n");
        return -1;
    }

    int i = 0;
    pthread_t thread;
    int threadNum = atoi(argv[0]);

    for (i = 0; i < threadNum; i++)
    {
        if (pthread_create(&thread, NULL, (void *)foo, NULL) != 0)
        {
            LOGE("pthread_create %d failed\n", i);
            return -1;
        }
        LOGD("pthread_create i=%d ok\n", i);
    }
    

    return 0;
}

static int cmd_playmp3(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    playmp3 [mp3_file]\n");
        return -1;
    }

    AudioGetOpsHandle()->play_file_start(argv[0], AUDIO_TYPE_MP3, 0);
    
	return 0;
}

static int cmd_settimezone(int argc, char *argv[])
{
    SYS_TIMEZONE_INFO_S stTimeZone;

    memset(&stTimeZone, 0, sizeof(stTimeZone));
    stTimeZone.zoneSec = 8 * 3600;
    SystemSetTimeZone(stTimeZone);
    
	return 0;
}

static int cmd_aosetvolume(int argc, char *argv[])
{
    if (argc != 1) {
        printf("Usage:\n");
        printf("    ao_setvolume [value]\n");
        return -1;
    }

    //extern HI_S32 ADPT_ACODEC_SetAOVol(HI_S32 s32Vol);
    ADPT_ACODEC_SetAOVol(atoi(argv[0]));

    return 0;
}

static int cmd_snappic(int argc, char *argv[])
{
    static int num = 0;
    int ret = 0;
    int outSize = 0;
    unsigned char *pBuf = NULL; 
    char fileName[32] = "";
    FILE *fp = NULL;
       
    ret = VideoGetOpsHandle()->get_snap_pic(&pBuf, &outSize);
    if (ret < 0) {
        LOGE("get_snap_pic failed\n");
        goto free_buf;
    }

    if (pBuf == NULL) {
        LOGE("pBuf is NULL\n");
        return -1;
    }
    
    snprintf(fileName, sizeof(fileName), "/sdcard/%02d_snap.jpg", num++);
    fp = fopen(fileName, "wb");
    if (fp == NULL) {
        LOGE("fopen %s failed\n", fileName);
        goto free_buf;
    }

    LOGD("%s outSize %d\n", fileName, outSize);
    fwrite(pBuf, outSize, 1, fp);
    
    fflush(fp);

close_file:    
    fclose(fp);

free_buf:
    if (pBuf != NULL) {
        free(pBuf);
    }
    
	return 0;
}

static int cmd_mcutime(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    mcutime [set/get]\n");
        return -1;
    }

    SYSTEM_TIME stSysTime;

    memset(&stSysTime, 0, sizeof(stSysTime));
    
    if (!strcmp(argv[0], "set")) {
        //stSysTime.year = 2019;
        stSysTime.year = atoi(argv[1]);
        stSysTime.month = 7;
        stSysTime.day = 18;
        stSysTime.hour = 18;
        stSysTime.minute = 2;
        stSysTime.second = 8;
        SocSubSetTime(stSysTime);
    } else if (!strcmp(argv[0], "get")) {
        SocSubGetTime(&stSysTime);
    }

    return 0;
}

static int cmd_wdgset(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    wdgset 0 ---disable wdt\n");
        printf("    wdgset 1 ---enable wdt\n");
        printf("    wdgset 2 ---feed wdt\n");
        return -1;
    }
    
    if (!strcmp(argv[0], "0")) {
        SocSubSetWdgState(0);
        SocSubSetMainSocStartDetectTime(0);
    } else if (!strcmp(argv[0], "1")) {
        SocSubSetWdgState(1);
        SocSubSetMainSocStartDetectTime(10);
    } else if (!strcmp(argv[0], "2")) {
        SocSubSetWdgFeed();
    }
    
    return 0;
}

static int cmd_mcuwifistate(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    mcuwifistate set [0/1]\n");
        printf("    mcuwifistate get\n");
        return -1;
    }

    char state = 0;
    
    if (!strcmp(argv[0], "set")) {
        if (argc != 2) {
            printf("mcuwifistate set [0/1]\n");
            return -1;
        }
        state = atoi(argv[1]);
        SocSubSetWifiState(state);
    } else if (!strcmp(argv[0], "get")) {
        SocSubGetWifiState(&state);
    }

    return 0;
}


static int cmd_mcuupdate(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    mcuupdate [file]\n");
        return -1;
    }

    PowerSetWakeLock("mcuupdate");

    McuUpdateInit("");
    
	unsigned char *pBuf = NULL;
	FILE *fp = NULL;
	int readLen = 0;
	int readOffset = 0;
	int pktSize = 0;

	pBuf = (unsigned char *)malloc(32 * 1024);
	if (pBuf == NULL) {
		printf("malloc failed\n");
		return -1;
	}
	
	memset(pBuf, 0, 32 * 1024);

	fp = fopen(argv[0], "rb");
	if (fp == NULL) {
		perror(argv[1]);
		free(pBuf);
		return -1;
	}

	while (1)
	{
		readLen = fread(pBuf + readOffset, 1, 1024, fp);
		if (readLen == 0) {
			fclose(fp);
			break;
		}
		readOffset += readLen;
		pktSize += readLen;
	}

	printf("pktSize: %d\n", pktSize);
	McuUpdateRun(pBuf, pktSize);
    free(pBuf);

    McuUpdateDeinit("");

    PowerSetWakeUnLock("mcuupdate");
    
    return 0;
}

static int cmd_wifiupdate(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    wifiupdate [file]\n");
        return -1;
    }

    int ret = 0;
    int pktSize = 0;
    struct stat statbuf;
    unsigned char *pBuf = NULL;
    FILE *fp = NULL;
    int readLen = 0;

    PowerSetWakeLock("wifiupdate");
    
    ret = lstat(argv[0], &statbuf);
    if (ret != 0) {
        LOGE("lstat %s failed\n", argv[0]);
        return -1;
    }
    
    pktSize = statbuf.st_size;
    ret = SocSubUpgradeStart(1, pktSize, "");
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == AV_SUCCESS, AV_FAILURE, "SocSubUpgradeStart");
   
	pBuf = (unsigned char *)malloc(32 * 1024);
	if (pBuf == NULL) {
		printf("malloc failed\n");
		return -1;
	}

	fp = fopen(argv[0], "rb");
	if (fp == NULL) {
		perror(argv[1]);
		LOGE("fopen %s failed\n", argv[0]);
        goto free_buffer;
	}

	while (1)
	{
	    memset(pBuf, 0, 32 * 1024);
		readLen = fread(pBuf, 1, 32 * 1024, fp);
		if (readLen == 0) {
			fclose(fp);
			break;
		}
		pktSize += readLen;

		ret = SocSubUpgradeTransmit(pBuf, readLen);
		if (ret != AV_SUCCESS) {
		    LOGE("SocSubUpgradeTransmit failed\n");
            goto free_buffer;
		}
	}

	printf("pktSize: %d\n", pktSize);
    
    SocSubUpgradeFinish("", "");
    
free_buffer:
    free(pBuf);
    pBuf = NULL;
close_fp:
    if (fp != NULL) {
        fclose(fp);
    }
free_lock:
    PowerSetWakeUnLock("wifiupdate");
    
    return 0;
}

static int cmd_netset(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    netset get\n");
        printf("    netset set [ip] [netmask] [gateway]\n");
        return -1;
    }

    NET_ATTR_INFO_S stNetAttrInfo;

    memset(&stNetAttrInfo, 0, sizeof(stNetAttrInfo));
    
    if (!strcmp(argv[0], "set")) {
        if (argc != 4) {
            printf("netset set [ip] [netmask] [gateway]\n");
            return -1;
        }
        snprintf(stNetAttrInfo.ip, sizeof(stNetAttrInfo.ip), "%s", argv[1]);
        snprintf(stNetAttrInfo.netmask, sizeof(stNetAttrInfo.netmask), "%s", argv[2]);
        snprintf(stNetAttrInfo.gateway, sizeof(stNetAttrInfo.gateway), "%s", argv[3]);
        NetSetAttrInfo("wlan0", stNetAttrInfo);
        
    } else if (!strcmp(argv[0], "get")) {
        NetGetAttrInfo("wlan0", &stNetAttrInfo);
        printf("ip: %s netmask: %s gw: %s\n", stNetAttrInfo.ip, stNetAttrInfo.netmask, stNetAttrInfo.gateway);
    }

    return 0;
}

static int cmd_mcuconfig(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    mcuconfig set soctimeout [value]\n");
        printf("    mcuconfig get\n");
        return -1;
    }

    SOCSUB_SYS_CONFIG_S stMcuSysConfig;

    memset(&stMcuSysConfig, 0, sizeof(stMcuSysConfig));
    
    if (!strcmp(argv[0], "set")) {
        if (argc < 2) {
            printf("mcuconfig set [0/1]\n");
            return -1;
        }

        SocSubGetSysConfig(&stMcuSysConfig);
        
        if (!strcmp(argv[1], "soctimeout")) {
            stMcuSysConfig.socStartMaxSecond = atoi(argv[2]);
            SocSubSetSysConfig(stMcuSysConfig);
        }
    } else if (!strcmp(argv[0], "get")) {
        SocSubGetSysConfig(&stMcuSysConfig);
        printf("socStartMaxSecond: %d\n", stMcuSysConfig.socStartMaxSecond);
        printf("pirAlarmSwith: %d\n", stMcuSysConfig.pirAlarmSwith);
        printf("pirAlarmInterval: %d\n", stMcuSysConfig.pirAlarmInterval);
        printf("pirSensibility: %d\n", stMcuSysConfig.pirSensibility);
        printf("batteryLevel1Cfg: %d\n", stMcuSysConfig.batteryLevel1Cfg);
        printf("batteryLevel2Cfg: %d\n", stMcuSysConfig.batteryLevel2Cfg);
        printf("batteryLevel3Cfg: %d\n", stMcuSysConfig.batteryLevel3Cfg);
        printf("keyResetTimeSeconds: %d\n", stMcuSysConfig.keyResetTimeSeconds);
        printf("stopchecksocalive: %d\n", stMcuSysConfig.stopchecksocalive);
        printf("longAlive: %d\n", stMcuSysConfig.longAlive);
    }

    return 0;
}

static int cmd_mcuver(int argc, char *argv[])
{
    char version[32] = "";

    SocSubGetVersion(version);
    printf("mcu_version: %s\n", version);

    return 0;
}

static int cmd_wifimode(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    wifimode ap/sta\n");
        return -1;
    }

    WIFI_MODE_ATTR_S stModeAttr;
    memset(&stModeAttr, 0, sizeof(stModeAttr));
    
    if (!strcmp(argv[0], "sta")) {
        stModeAttr.enMode = WIFI_MODE_STA;
        WifiSetMode(stModeAttr);
        WifiSetConnect("X6_BSP", "amba1234", WIFI_ENCRYPT_WPA_WPA2);
    } else if (!strcmp(argv[0], "ap")) {
        stModeAttr.enMode = WIFI_MODE_AP;\
        snprintf(stModeAttr.stApConfig.ssid, sizeof(stModeAttr.stApConfig.ssid), "%s", "B1");
        stModeAttr.stApConfig.channel = 13;
        stModeAttr.stApConfig.mode = WIFI_ENCRYPT_OPEN;
        WifiSetMode(stModeAttr);
    }

    return 0;
}

static int cmd_pingtest(int argc, char *argv[])
{
    if (argc != 3) {
        printf("Usage:\n");
        printf("    pingtest [addr] [count] [timeoutMs]\n");
        return -1;
    }

    NetPing(argv[0], atoi(argv[1]), atoi(argv[2]));
    
    return 0;
}

static int cmd_osdsettemp(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    osdsettemp [value]\n");
        printf("    osdsettemp stop [index]\n");
        return -1;
    }

    if (!strcmp(argv[0], "stop")) {
        if (argc == 2) {
            OsdDrawStop(atoi(argv[1]));
        }
    } else {
        float tempValue = 0.0;
        tempValue = (float)(atof(argv[0]));
        printf("tempValue: %0.2f\n", tempValue);
        
        OsdDrawSetTemperature(tempValue);
    }
    
    return 0;
}
static int cmd_sdcheck(int argc, char *argv[])
{
    if (SdCheckIsMount(SDCARD_DEVICE, SDCARD_MOUNT_PATH)) {
        LOGW("sd already mount!\n");
    } else {
        LOGW("sd dont't mount!\n");
    }
    return 0;
}

static int cmd_tempget(int argc, char *argv[])
{
    float tempValue = 0;
    SocSubGetTemperature(&tempValue);

    LOGD("tempValue  %0.1f\n", tempValue);
    
	return 0;
}
    

extern VIDEO_OPS_FUNC_S *VideoGetOpsHandle(void);
static int cmd_videotest(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    videotest [index]\n");
        return -1;
    }

    int index = atoi(argv[0]);

    switch (index)
    {
        case 1:
            VideoGetOpsHandle()->init();
            break;
        case 2:
            VideoGetOpsHandle()->deinit();
            break;
        default:
            printf("invalid index (%d)!\n", index);
            break;
    }
    
    return 0;
}


extern AUDIO_OPS_FUNC_S *AudioGetOpsHandle(void);\
static int cmd_audiotest(int argc, char *argv[])
{
    if (argc == 0) {
        printf("Usage:\n");
        printf("    audiotest [index]\n");
        return -1;
    }

    int index = atoi(argv[0]);

    switch (index)
    {
        case 1:
            AudioGetOpsHandle()->init();
            break;
        case 2:
            AudioGetOpsHandle()->deinit();
            break;
        default:
            printf("invalid index (%d)!\n", index);
            break;
    }
    
    return 0;
}

static int cmd_wifitty(int argc, char *argv[])
{
#ifdef HISYSLINK_ENABLE
    wifisoc_set_wifi_tty_enable();
#endif /* HISYSLINK_ENABLE */

	return 0;
}

static int cmd_himd(int argc, char *argv[])
{
    if (argc < 1) {
        printf("Usage:\n");
        printf("    himd [regaddr]\n");
        return -1;
    }

    HI_U32 u32Addr = 0;
    HI_U32 u32Value = 0;

    u32Addr = strtol(argv[0], NULL, 16);
    himd(u32Addr, &u32Value);
    LOGI("addr: 0x%x value:0x%x\n", u32Addr, u32Value);
    
    return 0;
}

static int cmd_himm(int argc, char *argv[])
{
    if (argc < 2) {
        printf("Usage:\n");
        printf("    himm [regaddr] [value]\n");
        return -1;
    }

    HI_U32 u32Addr = 0;
    HI_U32 u32Value = 0;

    u32Addr = strtol(argv[0], NULL, 16);
    u32Value = strtol(argv[1], NULL, 16);
    himm(u32Addr, u32Value);
    LOGI("addr: 0x%x value:0x%x\n", u32Addr, u32Value);
    
    return 0;
}

static int cmd_reset1(int argc, char *argv[])
{
    SystemReset();
	return 0;
}

void product_cmd_register(void)
{
#ifdef HISYSLINK_ENABLE
    HI_SYSLINK_CMD_RegOsCmd();
#endif /* HISYSLINK_ENABLE */

    osCmdReg(CMD_TYPE_EX, "gpioset", 5, (CMD_CBK_FUNC)cmd_gpioset);
    
    osCmdReg(CMD_TYPE_EX, "logset", 1, (CMD_CBK_FUNC)cmd_logset);
    osCmdReg(CMD_TYPE_EX, "tagset", 1, (CMD_CBK_FUNC)cmd_tagset);
    //osCmdReg(CMD_TYPE_EX, "ledset", 1, (CMD_CBK_FUNC)cmd_ledset);
//    osCmdReg(CMD_TYPE_EX, "waveset", 1, (CMD_CBK_FUNC)cmd_waveset);

//    osCmdReg(CMD_TYPE_EX, "pirset", 1, (CMD_CBK_FUNC)cmd_pirset);
//    osCmdReg(CMD_TYPE_EX, "videoset", 1, (CMD_CBK_FUNC)cmd_videoset);
//    osCmdReg(CMD_TYPE_EX, "osset", 1, (CMD_CBK_FUNC)cmd_osset);

//    osCmdReg(CMD_TYPE_EX, "daemon", 1, (CMD_CBK_FUNC)cmd_daemon);
//    osCmdReg(CMD_TYPE_EX, "playaudio", 1, (CMD_CBK_FUNC)cmd_playaudio);
//    osCmdReg(CMD_TYPE_EX, "gethostip", 1, (CMD_CBK_FUNC)cmd_gethostip);
    osCmdReg(CMD_TYPE_EX, "wakelock", 1, (CMD_CBK_FUNC)cmd_wakelock);
//    osCmdReg(CMD_TYPE_EX, "flashtest", 1, (CMD_CBK_FUNC)cmd_flashtest);
//    osCmdReg(CMD_TYPE_EX, "printtest", 1, (CMD_CBK_FUNC)cmd_printtest);
//    osCmdReg(CMD_TYPE_EX, "update", 1, (CMD_CBK_FUNC)app_sample);
#ifdef WIFI_TEST_MODE
      osCmdReg(CMD_TYPE_EX, "upgrade", 1, (CMD_CBK_FUNC)cmd_upgrade);
#endif      
//    osCmdReg(CMD_TYPE_EX, "upgrade2", 1, (CMD_CBK_FUNC)cmd_upgrade3);
//    osCmdReg(CMD_TYPE_EX, "iperf", 1, (CMD_CBK_FUNC)cmd_iperf);
#if 0
    osCmdReg(CMD_TYPE_EX, "getbattery", 1, (CMD_CBK_FUNC)cmd_getbattery);
    osCmdReg(CMD_TYPE_EX, "getwakeup", 1, (CMD_CBK_FUNC)cmd_getwakeup);
    osCmdReg(CMD_TYPE_EX, "getsockopt", 1, (CMD_CBK_FUNC)cmd_getsockopt);
    osCmdReg(CMD_TYPE_EX, "getadc", 1, (CMD_CBK_FUNC)cmd_getadc);
    osCmdReg(CMD_TYPE_EX, "setvideo", 1, (CMD_CBK_FUNC)cmd_setvideo);
    osCmdReg(CMD_TYPE_EX, "setstream", 1, (CMD_CBK_FUNC)cmd_setstream);
    osCmdReg(CMD_TYPE_EX, "color2grey", 1, (CMD_CBK_FUNC)cmd_color2grey);
    osCmdReg(CMD_TYPE_EX, "getwifistatus", 1, (CMD_CBK_FUNC)cmd_getwifistatus);
#endif
    osCmdReg(CMD_TYPE_EX, "sleepset", 1, (CMD_CBK_FUNC)cmd_sleepset);
//    osCmdReg(CMD_TYPE_EX, "threadset", 1, (CMD_CBK_FUNC)cmd_threadtest);
//    osCmdReg(CMD_TYPE_EX, "playmp3", 1, (CMD_CBK_FUNC)cmd_playmp3);
//    osCmdReg(CMD_TYPE_EX, "playg711a", 1, (CMD_CBK_FUNC)cmd_playg711a);
//    osCmdReg(CMD_TYPE_EX, "settimezone", 1, (CMD_CBK_FUNC)cmd_settimezone);
//    osCmdReg(CMD_TYPE_EX, "ao_setvolume", 1, (CMD_CBK_FUNC)cmd_aosetvolume);
//      osCmdReg(CMD_TYPE_EX, "snappic", 1, (CMD_CBK_FUNC)cmd_snappic);
//      osCmdReg(CMD_TYPE_EX, "mcutime", 1, (CMD_CBK_FUNC)cmd_mcutime);
    osCmdReg(CMD_TYPE_EX, "wdgset", 1, (CMD_CBK_FUNC)cmd_wdgset);
//    osCmdReg(CMD_TYPE_EX, "mcuwifistate", 1, (CMD_CBK_FUNC)cmd_mcuwifistate);
#ifdef PQ_ENABLE
	osCmdReg(CMD_TYPE_EX,"pq_control", 0, (CMD_CBK_FUNC)app_control);
#endif /* PQ_ENABLE */
    osCmdReg(CMD_TYPE_EX,"devtag", 0, (CMD_CBK_FUNC)cmd_devtag);
//    osCmdReg(CMD_TYPE_EX,"mcuupdate", 0, (CMD_CBK_FUNC)cmd_mcuupdate);
//    osCmdReg(CMD_TYPE_EX,"netset", 0, (CMD_CBK_FUNC)cmd_netset);
//    osCmdReg(CMD_TYPE_EX,"mcuconfig", 0, (CMD_CBK_FUNC)cmd_mcuconfig);
    osCmdReg(CMD_TYPE_EX,"mcuver", 0, (CMD_CBK_FUNC)cmd_mcuver);
    osCmdReg(CMD_TYPE_EX,"ubootenv", 0, (CMD_CBK_FUNC)cmd_ubootenv);
//    osCmdReg(CMD_TYPE_EX,"wifimode", 0, (CMD_CBK_FUNC)cmd_wifimode);
//    osCmdReg(CMD_TYPE_EX,"pingtest", 0, (CMD_CBK_FUNC)cmd_pingtest);
//    osCmdReg(CMD_TYPE_EX,"osdsettemp", 0, (CMD_CBK_FUNC)cmd_osdsettemp);
    osCmdReg(CMD_TYPE_EX,"videotest", 0, (CMD_CBK_FUNC)cmd_videotest);
    osCmdReg(CMD_TYPE_EX,"audiotest", 0, (CMD_CBK_FUNC)cmd_audiotest);
//    osCmdReg(CMD_TYPE_EX,"sdcheck", 0, (CMD_CBK_FUNC)cmd_sdcheck);
//    osCmdReg(CMD_TYPE_EX,"tempget", 0, (CMD_CBK_FUNC)cmd_tempget);
    osCmdReg(CMD_TYPE_EX,"wifitty", 0, (CMD_CBK_FUNC)cmd_wifitty);

    extern hi_u32 hi_syslink_sample(hi_u32 argc, hi_char **argv);
//    osCmdReg(CMD_TYPE_EX, "hisyslink", 1, (CmdCallBackFunc)hi_syslink_sample);
//    osCmdReg(CMD_TYPE_EX,"wifiupdate", 0, (CMD_CBK_FUNC)cmd_wifiupdate);
//    osCmdReg(CMD_TYPE_EX,"himd", 0, (CMD_CBK_FUNC)cmd_himd);
//    osCmdReg(CMD_TYPE_EX,"himm", 0, (CMD_CBK_FUNC)cmd_himm);
    osCmdReg(CMD_TYPE_EX,"reboot", 0, (CMD_CBK_FUNC)cmd_reset1);
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */

